package com.xbongbong.sso.service.imp;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.EnableEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.pro.enums.errorcodes.SsoErrorCodeEnum;
import com.xbongbong.sso.constant.SsoConstant;
import com.xbongbong.sso.domain.entity.OauthConnectorEntity;
import com.xbongbong.sso.enums.AccountRuleEnum;
import com.xbongbong.sso.help.OauthConnectorHelp;
import com.xbongbong.sso.model.OauthConnectorModel;
import com.xbongbong.sso.pojo.OauthConnectorPojo;
import com.xbongbong.sso.pojo.dto.OauthConnectorAddDTO;
import com.xbongbong.sso.pojo.dto.OauthConnectorDeleteDTO;
import com.xbongbong.sso.pojo.dto.OauthConnectorDetailDTO;
import com.xbongbong.sso.pojo.dto.OauthConnectorEnableDTO;
import com.xbongbong.sso.pojo.dto.OauthConnectorListDTO;
import com.xbongbong.sso.pojo.dto.OauthConnectorUpdateDTO;
import com.xbongbong.sso.pojo.vo.OauthConnectorAddVO;
import com.xbongbong.sso.pojo.vo.OauthConnectorDetailVO;
import com.xbongbong.sso.pojo.vo.OauthConnectorEnableVO;
import com.xbongbong.sso.pojo.vo.OauthConnectorExistVO;
import com.xbongbong.sso.pojo.vo.OauthConnectorListVO;
import com.xbongbong.sso.pojo.vo.OauthConnectorUpdateVO;
import com.xbongbong.sso.service.OauthConnectorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 授权连接器
 */
@Service("oauthConnectorService")
public class OauthConnectorServiceImpl implements OauthConnectorService {
    private static final Logger LOG = LoggerFactory.getLogger(OauthConnectorServiceImpl.class);

    @Resource
    private OauthConnectorModel oauthConnectorModel;
    @Resource
    private OauthConnectorHelp oauthConnectorHelp;


    @Override
    public OauthConnectorAddVO add(OauthConnectorAddDTO oauthConnectorAddDTO) throws XbbException {
        String corpid = oauthConnectorAddDTO.getCorpid();
        Integer enable = oauthConnectorAddDTO.getEnable();
        Integer type = oauthConnectorAddDTO.getType();
        String name = oauthConnectorAddDTO.getName();
        String connectorSign = oauthConnectorAddDTO.getConnectorSign();
        String clientId = oauthConnectorAddDTO.getClientId();
        String clientSecret = oauthConnectorAddDTO.getClientSecret();
        Integer accountRule = oauthConnectorAddDTO.getAccountRule();
        Integer directSsoLogin = oauthConnectorAddDTO.getDirectSsoLogin();

        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("del", 0);
        Integer count = oauthConnectorModel.getEntitiesCount(param);
        if (count >= SsoConstant.CONNECTOR_MAX_NUM) {
            throw new XbbException(SsoErrorCodeEnum.API_ERROR_273001.getCode(), SsoErrorCodeEnum.API_ERROR_273001.getMsg());
        }

        if (oauthConnectorHelp.connectorSignRepeat(null, connectorSign)) {
            throw new XbbException(SsoErrorCodeEnum.API_ERROR_273014);
        }

        OauthConnectorEntity oauthConnector = new OauthConnectorEntity();
        oauthConnector.setCorpid(corpid);
        oauthConnector.setEnable(enable);
        oauthConnector.setType(type);
        oauthConnector.setName(name);
        oauthConnector.setConnectorSign(connectorSign);
        oauthConnector.setClientId(clientId);
        oauthConnector.setClientSecret(clientSecret);
        oauthConnector.setAccountRule(accountRule);
        oauthConnector.setDirectSsoLogin(directSsoLogin);
        oauthConnectorModel.insert(oauthConnector);
        OauthConnectorAddVO oauthConnectorAddVO = new OauthConnectorAddVO();
        oauthConnectorAddVO.setDataId(oauthConnector.getId());
        return oauthConnectorAddVO;
    }

    @Override
    public OauthConnectorUpdateVO update(OauthConnectorUpdateDTO oauthConnectorUpdateDTO) throws XbbException {
        String connectorSign = oauthConnectorUpdateDTO.getConnectorSign();
        if (connectorSign != null && oauthConnectorHelp.
                connectorSignRepeat(oauthConnectorUpdateDTO.getId(), connectorSign)) {
            throw new XbbException(SsoErrorCodeEnum.API_ERROR_273014);
        }

        OauthConnectorEntity oauthConnector = new OauthConnectorEntity();
        oauthConnector.setId(oauthConnectorUpdateDTO.getId());
        oauthConnector.setEnable(oauthConnectorUpdateDTO.getEnable());
        oauthConnector.setType(oauthConnectorUpdateDTO.getType());
        oauthConnector.setName(oauthConnectorUpdateDTO.getName());
        oauthConnector.setConnectorSign(connectorSign);
        oauthConnector.setClientId(oauthConnectorUpdateDTO.getClientId());
        oauthConnector.setClientSecret(oauthConnectorUpdateDTO.getClientSecret());
        oauthConnector.setAccountRule(oauthConnectorUpdateDTO.getAccountRule());
        oauthConnector.setDirectSsoLogin(oauthConnectorUpdateDTO.getDirectSsoLogin());
        oauthConnector.setUpdateTime(DateTimeUtil.getInt());
        oauthConnectorModel.update(oauthConnector);
        OauthConnectorUpdateVO oauthConnectorUpdateVO = new OauthConnectorUpdateVO();
        oauthConnectorUpdateVO.setDataId(oauthConnector.getId());
        return oauthConnectorUpdateVO;
    }

    @Override
    public OauthConnectorListVO list(OauthConnectorListDTO oauthConnectorListDTO) throws XbbException {
        String corpid = oauthConnectorListDTO.getCorpid();
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("del", 0);
        List<OauthConnectorEntity> oauthConnectors = oauthConnectorModel.findEntities(param);
        List<OauthConnectorPojo> list = new ArrayList<>();
        for (OauthConnectorEntity oauthConnector : oauthConnectors) {
            OauthConnectorPojo oauthConnectorPojo = oauthConnectorHelp.oauthConnectorPojoFormat(oauthConnector);
            list.add(oauthConnectorPojo);
        }
        OauthConnectorListVO oauthConnectorListVO = new OauthConnectorListVO();
        oauthConnectorListVO.setList(list);
        return oauthConnectorListVO;
    }

    @Override
    public OauthConnectorDetailVO detail(OauthConnectorDetailDTO oauthConnectorDetailDTO) throws XbbException {
        String corpid = oauthConnectorDetailDTO.getCorpid();
        Long id = oauthConnectorDetailDTO.getId();
        OauthConnectorEntity oauthConnector = oauthConnectorModel.getByKey(id, corpid);
        OauthConnectorPojo oauthConnectorPojo = oauthConnectorHelp.oauthConnectorPojoFormat(oauthConnector);
        OauthConnectorDetailVO oauthConnectorDetailVO = new OauthConnectorDetailVO();
        oauthConnectorDetailVO.setOauthConnector(oauthConnectorPojo);
        return oauthConnectorDetailVO;
    }

    @Override
    public OauthConnectorEnableVO enable(OauthConnectorEnableDTO oauthConnectorEnableDTO) throws XbbException {
        OauthConnectorUpdateDTO oauthConnectorUpdateDTO = new OauthConnectorUpdateDTO();
        BeanUtil.copyProperties(oauthConnectorEnableDTO, oauthConnectorUpdateDTO);
        update(oauthConnectorUpdateDTO);
        OauthConnectorEnableVO oauthConnectorEnableVO = new OauthConnectorEnableVO();
        oauthConnectorEnableVO.setDataId(oauthConnectorEnableDTO.getId());
        return oauthConnectorEnableVO;
    }

    @Override
    public BaseVO delete(OauthConnectorDeleteDTO oauthConnectorDeleteDTO) throws XbbException {
        Long id = oauthConnectorDeleteDTO.getId();
        String corpid = oauthConnectorDeleteDTO.getCorpid();
        OauthConnectorEntity oauthConnector = oauthConnectorModel.getByKey(id, corpid);
        if (Objects.isNull(oauthConnector) || !Objects.equals(oauthConnector.getDel(), DelEnum.NORMAL.getDel())) {
            throw new XbbException(SsoErrorCodeEnum.API_ERROR_273003.getCode(), SsoErrorCodeEnum.API_ERROR_273003.getMsg());
        }
        oauthConnectorModel.delete(id, corpid);
        return new BaseVO();
    }

    @Override
    public OauthConnectorExistVO exist(BaseDTO baseDTO) throws XbbException {
        String corpid = baseDTO.getCorpid();
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("enable", EnableEnum.OPEN.getCode());
        param.put("accountRule", AccountRuleEnum.CUSTOMIZE.getCode());
        param.put("del", DelEnum.NORMAL.getDel());
        Integer count = oauthConnectorModel.getEntitiesCount(param);
        boolean hasConnector = count > 0 ? true : false;
        OauthConnectorExistVO oauthConnectorExistVO = new OauthConnectorExistVO();
        oauthConnectorExistVO.setHasConnector(hasConnector);
        return oauthConnectorExistVO;
    }
}
